package com.example.testandroid;

import java.util.List;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service{

	

	/**
	 * 监听程序是否在前后台运行Service
	 * @Description: 监听程序是否在前后台运行Service

	 * @FileName: AppStatusService.java 

	 * @Package com.test.service

	 * @Author Hanyonglu

	 * @Date 2012-4-13 下午04:13:47 

	 * @Version V1.0
	 */
	
	    private static final String TAG = "AppStatusService"; 
	    private ActivityManager activityManager; 
	    private String packageName;
	    private boolean isStop = false;
	    private static boolean flag = false;
	    public static Context context;
	    
	    @Override
	    public IBinder onBind(Intent intent) {
	        // TODO Auto-generated method stub
	        return null;
	    }
	    
	    @Override
	    public int onStartCommand(Intent intent, int flags, int startId) {
	        // TODO Auto-generated method stub
	        
	        System.out.println("启动服务");
	        
	        Thread t = new MyThread();
	        t.setPriority(Thread.MAX_PRIORITY);
	        //t.start();
	        IntentFilter filter_system = new IntentFilter();  
	        filter_system.addAction(Intent.ACTION_POWER_CONNECTED);  
	        registerReceiver(systemReceiver, filter_system);
	        
	        return super.onStartCommand(intent, flags, startId);
	    }
	    
	    private BroadcastReceiver systemReceiver = new BroadcastReceiver() {  
	          
	        @Override  
	        public void onReceive(Context context, Intent intent) {  
	            
	            if(intent.getAction().equals(Intent.ACTION_POWER_CONNECTED)){  
	                String msg = intent.getStringExtra("msg");  
	                Toast.makeText(context, "充电中...", Toast.LENGTH_SHORT).show();  
	            }  
	        }  
	    };
	    
	    /**
	     * 程序是否在前台运行
	     * @return
	     */
	    public boolean isAppOnForeground() { 
	        // Returns a list of application processes that are running on the device 
	    	activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE); 
	        packageName = this.getPackageName(); 
	        List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); 
	        if (appProcesses == null) return false; 
	        
	        for (RunningAppProcessInfo appProcess : appProcesses) { 
	            // The name of the process that this object is associated with. 
	            if (appProcess.processName.equals(packageName) 
	                    && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { 
	                return true; 
	            } 
	        } 
	        RunnableDemo t1 = new RunnableDemo();
	        new Thread(t1).start();
	        
	        return false; 
	    } 
	    
	    @Override
	    public void onDestroy() {
	        // TODO Auto-generated method stub
	        super.onDestroy();
	        Log.v(TAG,"service stopped");
	        Intent i= new Intent(this,MyService.class);
	        this.startService(i);
	        isStop = true;
	    }
	    
//	    // 显示Notification
//	    public void showNotification() {
//	        // 创建一个NotificationManager的引用
//	        NotificationManager notificationManager = (
//	                NotificationManager)getSystemService(
//	                        android.content.Context.NOTIFICATION_SERVICE);
//	        
//	        // 定义Notification的各种属性
//	        Notification notification = new Notification(
//	                R.drawable.icon,"阅读器", 
//	                System.currentTimeMillis());
//	        // 将此通知放到通知栏的"Ongoing"即"正在运行"组中
//	        notification.flags |= Notification.FLAG_ONGOING_EVENT;
//	        // 点击后自动清除Notification
//	        notification.flags |= Notification.FLAG_AUTO_CANCEL;
//	        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//	        notification.defaults = Notification.DEFAULT_LIGHTS;
//	        notification.ledARGB = Color.BLUE;
//	        notification.ledOnMS = 5000;
//	                
//	        // 设置通知的事件消息
//	        CharSequence contentTitle = "阅读器显示信息"; // 通知栏标题
//	        CharSequence contentText = "推送信息显示，请查看……"; // 通知栏内容
//	        
//	        Intent notificationIntent = new Intent(AppManager.context,AppManager.context.getClass());
//	        notificationIntent.setAction(Intent.ACTION_MAIN);
//	        notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
//	        PendingIntent contentIntent = PendingIntent.getActivity(
//	                AppManager.context, 0, notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);
//	        notification.setLatestEventInfo(
//	                AppManager.context, contentTitle, contentText, contentIntent);
//	        // 把Notification传递给NotificationManager
//	        notificationManager.notify(0, notification);
//	    }
	    class MyThread extends Thread{
	    	public void run() { 
                try { 
                    while (!isStop) { 
                        //Thread.sleep(500); 
                        
                        if (isAppOnForeground()) { 
                            Log.v(TAG, "前台运行"+flag);
                        	if(flag) Log.v(TAG, "重新运行..........");
                        	//Toast.makeText(context, "resume", Toast.LENGTH_LONG);
                        	flag = false;
                        } else { 
                        	flag = true;
                            Log.v(TAG, "后台运行"+flag);
                            //showNotification();
                        	
                        	Toast.makeText(context, "进入后台", Toast.LENGTH_LONG);
                        } 
                    } 
                } catch (Exception e) { 
                    e.printStackTrace(); 
                } 
	    	}
	    }
	}